app.controller('homeCtrl',['$scope','homeService','$modal',function($scope,homeService,$modal) {
    $scope.legend = [ "下单笔数","下单人数","下单金额","支付笔数","支付人数","支付金额"];
    $scope.item = [ '--','--','--','--','--','--','--','--','--','--','--','--' ];
    $scope.data = [
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    ];

    $scope.homeTableVO={
        month:new Date().getMonth()+1,
        year:new Date().getFullYear()
    };
    $scope.months=[];
    $scope.years=[];

    var getYear=function(){
        for(var i=2015;i<$scope.homeTableVO.year;i++){
            $scope.years.push(i+1);
        }
    };

    var getMonth=function(){
        $scope.months=[];
        var nowYear=new Date().getFullYear();
        $scope.homeTableVO.month=($scope.homeTableVO.year==nowYear)?(new Date().getMonth()+1):12;
        for(var i=0;i<$scope.homeTableVO.month;i++){
            $scope.months.push(i+1);
        }
    };

    getYear();
    getMonth();

    //查询订单图表信息
    $scope.queryOrdersInfo=function(){
        homeService.queryOrdersInfo($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.item=result.data.item;
                    $scope.data=result.data.data;
                    break;
                default :
                    break;
            }
        })
    };

    $scope.queryOrdersInfo();

    //查询商品销量
    $scope.queryForGoods=function(){
        homeService.queryForGoods($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.goods=result.data;
                    break;
                default :
                    break;
            }
        })
    };

    $scope.queryForGoods();

    $scope.changeMonth=function(){
        $scope.queryOrdersInfo();
        $scope.queryForGoods();
        $scope.queryIncome();
    };

    $scope.changeYear=function(){
        getMonth();
        $scope.queryOrdersInfo();
        $scope.queryForGoods();
        $scope.queryIncome();
    };

    $scope.changeShopId=function(){
        $scope.queryOrdersInfo();
        $scope.queryForGoods();
    };



    //查询统计信息
    $scope.header={
        totalCount:0,
        totalOrders:0,
        totalPay:0,
        totalTransaction:0.00
    };
    $scope.queryHomeHeader=function(){
        homeService.queryHomeHeader($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.header=result.data;
                    break;
                default :
                    break;
            }
        })
    };

    $scope.queryHomeHeader();

    //查找店铺
    $scope.shop={};
    $scope.queryAllShop=function(){
        homeService.queryAllShop($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.shop=result.data;
                    break;
                default :
                    break;
            }
        })
    };

    $scope.queryAllShop();

    //查询销售排行
    $scope.queryIncome=function(){
        homeService.queryIncome($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.income=result.data;
                    break;
                default :
                    break;
            }
        })
    };

    $scope.queryIncome();


    //查询连锁品牌信息
    $scope.queryChain = function () {
        homeService.queryChain($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.chain = result.data;
                    if( $scope.chain.auditStatus==0 || $scope.chain.auditStatus==1){
                        $scope.openPopup();
                    }
                    break;
                default:
                    break;
            }
        })
    }

    $scope.queryChain();


    //打开弹窗
    $scope.openPopup = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/popup/popup.html',
            controller: 'popupCtrl',
            size: 'md',
            backdrop:"static",
            scope:$scope,
            resolve: {
                data: function () {
                    return  $scope.chain;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };



}]);

app.controller('popupCtrl', ['$scope', '$modalInstance','toaster','data',
    function($scope, $modalInstance,toaster,data) {

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

    }
]);


app.directive('line', function() {
    return {
        scope: {
            id: "@",
            legend: "=",
            item: "=",
            data: "="
        },
        restrict: 'E',
        template: '<div style="height:400px;width: 100%;"></div>',
        replace: true,
        link: function($scope, element, attrs, controller) {
            var option = {
                // 提示框，鼠标悬浮交互时的信息提示
                tooltip: {
                    trigger: 'axis',
                    textStyle:{
                        color:'#fff'
                    },
                    fontSize:12,
                    padding:15,
                    show:true,
                    formatter: function(params) {
                        var res='';
                        var date='';
                        var ordersCount='';
                        var ordersUserCount='';
                        var ordersMoney='';
                        var payOrdersCount='';
                        var payUserCount='';
                        var payMoney='';
                        for(var i=0;i<params.length;i++){
                            date=params[0].name+'</br>';
                            if(params[i].seriesIndex==0){
                                ordersCount=params[i].seriesName+":"+params[i].data+'单'+'</br>';
                            }else if(params[i].seriesIndex==1){
                                ordersUserCount=params[i].seriesName+":"+params[i].data+'人'+'</br>';
                            }else if(params[i].seriesIndex==2){
                                ordersMoney=params[i].seriesName+":￥"+params[i].data+'</br>';
                            }else if(params[i].seriesIndex==3){
                                payOrdersCount=params[i].seriesName+":"+params[i].data+'单'+'</br>';
                            }else if(params[i].seriesIndex==4){
                                payUserCount=params[i].seriesName+":"+params[i].data+'人'+'</br>';
                            }else if(params[i].seriesIndex==5){
                                payMoney=params[i].seriesName+":￥"+params[i].data+'</br>';
                            }
                        }
                        res=date+ordersCount+ordersUserCount+ordersMoney+payOrdersCount+payUserCount+payMoney;
                        return res
                    }
                },
                // 图例
                legend: {
                    data: $scope.legend
                },
                // 横轴坐标轴
                xAxis: [{
                    type: 'category',
                    splitLine: {
                        show: false
                    },
                    data: $scope.item,

                }],
                // 纵轴坐标轴
                yAxis: [{
                    axisLine: {
                        show: false
                    },
                    splitLine: {
                        show:false,
                        lineStyle: {
                            type: 'dotted'
                        }
                    },
                    type: 'value'
                }],
                // 数据内容数组
                series: function(){
                    var serie=[];
                    for(var i=0;i<$scope.legend.length;i++){
                        var item = {
                            name : $scope.legend[i],
                            type: 'line',
                            symbol: 'circle',
                            smooth: true,
                            data: $scope.data[i]
                        };
                        serie.push(item);
                    }
                    return serie;
                }()
            };
            var myChart = echarts.init(document.getElementById($scope.id),'macarons');
            myChart.setOption(option);

            $scope.$watch('item',function(){
                option.xAxis=[{type: 'category', data: $scope.item}];
                var serie=[];
                for(var i=0;i<$scope.legend.length;i++){
                    var item = {
                        name : $scope.legend[i],
                        type: 'line',
                        symbol: 'circle',
                        smooth: true,
                        data: $scope.data[i]
                    };
                    serie.push(item);
                }
                option.series=serie;
                myChart.clear();
                myChart.setOption(option,true);
            })
        }
    };
});